
class MyContentContainer < Thing

  attribute :content_type
  attribute :items_per_page

  def recieve e
    ['items_per_page'].each { |a|
      if e.params[form_id a]
        self.send(a+'=', e.params[form_id a].to_i)
      end
    }

    ['content_type'].each { |a|
      if e.params[form_id a]
        self.send(a+'=', e.params[form_id a])
      end
    }
  end


  def creator e
    s = "<p>Stránkuj: "

    types = Hash[*ContentTemplate.all.collect { |t|
          [t.template_class, t.template_title]
        }.flatten]


    content_sel = Select.new({
        :name => form_id(:content_type)},
        types
      )

    content_sel.value = content_type

    s << content_sel.to_s

    s << ", Po: "

    s << Element.new(
        :input,
        :name => form_id(:items_per_page),
        :value => items_per_page,
        :size => 3
      ).to_s


    s + '</p>'
  end

  def render e
    s = ''

#    recieve e

    if content_type
      s << "<form method=\"post\"><p>"
      s << Element.new(
        :input,
        :type => :submit,
        :name => form_id(:add),
        :value => '+ '+content_type
      ).to_s
      s << "</form></p>"
      s << display_content_creator(e)
    end

    
    #page = e.params['page']
    if content_type
      s << (show e)
    end
    s
  end

  def show e
    action = e.env[:unprocessed_tail][0]
    if action == 'show'
      show_one e
    elsif action == 'edit'
      edit_one e
    else
      show_list e
    end
  end

  def edit_one e
     display_content_creator e, e.env[:unprocessed_tail].last
  end

  def show_one e
    if thing = content_class.first(:identity => e.env[:unprocessed_tail].last)
      thing.render e
    else
      "not found"
    end
  end

  def show_list e
    s = ''
    page = e.env[:unprocessed_tail].last.to_i || 1
    content = content_class.all()
    len = content.length
    if page <= 0 || page > len
      page = 1
    end
    list(page) { |c|
      s << "<a href=\"#{e.sublink('show/'+c.identity.to_s)}\">#{c.title}</a> "
      s << "[<a href=\"#{e.sublink('edit/'+c.identity.to_s)}\">edit</a>]<br/>"
    }
    s << pager(e, page, content)
    s
  end

  def display_content_creator e, item_identity = nil
    s = ''
    item = nil

    if item_identity || e.params[form_id(:add)] || e.params[form_id(:adding)]
      if item_identity
        item = content_class.first(:identity => item_identity)
      elsif item_id = e.params[form_id(:item_id)]
        item = Thing.to_const(content_type).get!(item_id)
      else e.params[form_id(:adding)]
        if ContentTemplate.first(:template_class => content_type)
          item = ContentTemplate.first(:template_class => content_type).clone_thing
        else
          raise "Could not find template definition for #{content_type}."
        end
      end

      item.recieve e
      item.each_thing { |t| t.show_window false }

      s << "<div class=\"pagerInner\">"

      form = item.creator_form(e)


      if !item.new?
        form.append_child Element.new(
            :input,
            :type => :hidden,
            :name => form_id(:item_id),
            :value => item.id
          ).to_s
      end

      form.append_child Element.new(
          :input,
          :type => :hidden,
          :name => form_id(:adding)
        )

      s << form.to_s

      s << "</div>"
    end
    s
  end

  def pager e, at_page, content
    return '' if !content_class
    len = content.length
    s = ''
    s << (1..(len/items_per_page).ceil).collect { |page|
      if page == at_page
        Element.new(:b, page.to_s, :class => 'actualPage')
      elsif page == 1
        Element.new(:a, page.to_s, :href => e.sublink(''))
      else
        Element.new(:a, page.to_s, :href => e.sublink(page.to_s))
      end
    }.join(", ")
    '<p>'+s+'</p>'
  end

  def list page = 1, &block
    if content_type
      content_class.all(:offset => page.to_i-1, :limit => items_per_page.to_i).each(&block)
    end
  end

  def content_class
    if content_type
      self.class.to_const content_type
    end
  end

end
